{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ca2d29f2",
   "metadata": {},
   "source": [
    "## numpy数组形状的操纵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d8bbe55c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ce6bed8",
   "metadata": {},
   "source": [
    "1. reshape不会修改原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6d24a102",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 5, 4, 5, 5, 2],\n",
       "       [4, 4, 3, 1, 4, 4]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.random.randint(0,6,size=(2,6))\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "712a0eb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 5, 4, 5],\n",
       "       [5, 2, 4, 4],\n",
       "       [3, 1, 4, 4]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.reshape((3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f5f5e911",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 5, 4, 5, 5, 2],\n",
       "       [4, 4, 3, 1, 4, 4]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用reshape后的原数组a1的形状并没有改变\n",
    "a1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7163274",
   "metadata": {},
   "source": [
    "2. 使用resize函数，会改变原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9004ab4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 0, 1, 2, 4, 3],\n",
       "       [1, 4, 0, 5, 4, 2]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = np.random.randint(0,6,size=(2,6))\n",
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "89c33fe7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#执行后没有返回任何值\n",
    "a2.resize((3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "78866e31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 0, 1, 2],\n",
       "       [4, 3, 1, 4],\n",
       "       [0, 5, 4, 2]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "feb64ede",
   "metadata": {},
   "source": [
    "3. flatten和ravel函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd99c318",
   "metadata": {},
   "source": [
    "* faltten和ravel将数组转换为一维数组.两者返回值的操作不一致。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6365767b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 1, 2, 0, 2],\n",
       "       [0, 2, 4, 0, 3],\n",
       "       [3, 0, 3, 2, 5]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3 = np.random.randint(0,6,size=(3,5))\n",
    "a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1cabc227",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([100,   1,   2,   0,   2,   0,   2,   4,   0,   3,   3,   0,   3,\n",
       "         2,   5])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4 = a3.flatten()\n",
    "a4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "152488bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 1, 2, 0, 2],\n",
       "       [0, 2, 4, 0, 3],\n",
       "       [3, 0, 3, 2, 5]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#执行flatten之后原数组没有改变\n",
    "a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "31764a45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 1, 2, 0, 2, 0, 2, 4, 0, 3, 3, 0, 3, 2, 5])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a5 = a3.ravel()\n",
    "a5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1d5bf8be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 1, 2, 0, 2],\n",
       "       [0, 2, 4, 0, 3],\n",
       "       [3, 0, 3, 2, 5]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "783a6415",
   "metadata": {},
   "outputs": [],
   "source": [
    "#两方法的区别在于拷贝.ravel实际上是一种视图，修改视图中的值就会修改原数组中的值。flatten就不会存在这个问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "05d107a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100   1   2   0   2   0   2   4   0   3   3   0   3   2   5]\n",
      "[[100   1   2   0   2]\n",
      " [  0   2   4   0   3]\n",
      " [  3   0   3   2   5]]\n"
     ]
    }
   ],
   "source": [
    "a5[0]=100\n",
    "print(a5)\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc5807ad",
   "metadata": {},
   "source": [
    "## 总结\n",
    "1. reshape和resize都可以重新定义形状。resize直接修改数组本身，reshape不能。\n",
    "2. flatten和ravel都能将数组变成一维数组，并且变形本身不会修改原数组。两者的区别在于，flatten的返回值修改某一个值也不会修改原数组。但ravel本质上是一种视图(View),修改某一个值会导致原数组的修改。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
